package dayong.com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import conn.Connection1;
import conn.userType;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class JsonpServlet
 */
@WebServlet(description = "Jsonp跨域请求", urlPatterns ="/JsonpServlet")
public class JsonpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JsonpServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//this.doPost(request, response); 
		
		boolean isSuccess = false ;
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String flag = request.getParameter("flag");  
		String callback = request.getParameter("callback");    
		PrintWriter out = response.getWriter(); 
		Connection1 conn =new Connection1();
		List<userType> list  = conn.getUserType();
		Gson gson= new Gson();
		String json = gson.toJson(list);
		JSONObject jsonRes =  new JSONObject();  
		switch(flag){  
		case "1":  
			//jsonRes.put("text","hello,你真的666");  //存值
			//String res= callback+"("+jsonRes.toString()+")";  //向前端传字符串
			//String res= callback+"("+isSuccess+")";   //向前端传boolean值
			String res= callback+"("+json.toString()+")"; // 向前端传json
			out.print(res);  
			out.close();  
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
